package com.zhbr.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zhbr.R;
import com.zhbr.entity.TGdfwPqgd;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderCardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<TGdfwPqgd> mList;

    public OrderCardAdapter(Context context, List<TGdfwPqgd> list){
        this.mContext=context;
        this.mList =list;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==0){
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_layout,parent,false);
            return new DataViewHolder(view);
        }else {
            View view  = LayoutInflater.from(mContext).inflate(R.layout.fonter_item_layout,parent,false);
            FooterHolder footerHolder = new FooterHolder(view);
            return footerHolder;
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof DataViewHolder){
            DataViewHolder dataViewHolder = (DataViewHolder) holder;
            TGdfwPqgd TGdfwPqgd = mList.get(position);
            dataViewHolder.tv_item_nickname.setText(TGdfwPqgd.getBxr());
            dataViewHolder.tv_item_phone.setText(TGdfwPqgd.getObjId());
            dataViewHolder.tv_item_desc.setText(TGdfwPqgd.getBxnr());
        }else if (holder instanceof FooterHolder){
            //底部“加载更多”item （等待动画用一个gif去实现）
            Glide.with(mContext)
                    .load(R.mipmap.load)
                    .into(((FooterHolder) holder).ivLoad);
        }

    }


    @Override
    public int getItemCount() {
        return mList.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == mList.size()){
            return 1;
        }else{
            return 0;
        }
    }

    //外部调用 刷新数据
    public void updateDataOver(){

        notifyDataSetChanged();
    }

    static class DataViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView tv_item_nickname;
        public TextView tv_item_phone;
        public TextView tv_item_desc;
        public DataViewHolder(View v) {
            super(v);
            tv_item_nickname = (TextView)v.findViewById(R.id.tv_item_nickname);
            tv_item_phone = (TextView)v.findViewById(R.id.tv_item_phone);
            tv_item_desc = (TextView)v.findViewById(R.id.tv_item_desc);
        }
    }


    //底部"加载更多"item viewholder
    public class FooterHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_load)
        ImageView ivLoad;

        public FooterHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
